/**
InfoExportFunction.java
myapp
2012-9-17下午5:34:10
@author 屈甲康
@QQ285799123
mailto:qujiakang@126.com
**/
package org.jackysoft.osworkflow.function;

import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.jackysoft.entity.Attachment;
import org.jackysoft.entity.HTMLDocument;
import org.jackysoft.entity.HibernateWorkflowEntry;
import org.jackysoft.entity.InfoExport;
import org.jackysoft.entity.OfficeDocument;
import org.jackysoft.entity.User;
import org.jackysoft.entity.Variable;
import org.jackysoft.opensymphony.workflow.WorkflowException;
import org.jackysoft.opensymphony.workflow.util.AbstractFunctionProvider;
import org.jackysoft.service.InfoExportService;
import org.jackysoft.service.UserService;
import org.jackysoft.service.VariableService;
import org.jackysoft.util.WorkflowConstant;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;


@Component
public class InfoExportFunction extends AbstractFunctionProvider {

	private InfoExportService service;
	private UserService userService;
	@Override
	public void execute(Map<String, Object> transientVars,
			Map<String, String> args, VariableService vservice)
			throws WorkflowException {
		HibernateWorkflowEntry entry  = (HibernateWorkflowEntry) transientVars.get("entry");
		Variable starter     = vservice.get(entry.getId(), WorkflowConstant.STARTER_USERID.toString());
		Variable start_date  = vservice.get(entry.getId(), WorkflowConstant.START_DATE.toString());
		Variable form_title  = vservice.get(entry.getId(), WorkflowConstant.FORM_TITLE.toString());
		
		DateTime scal = start_date.getDateTime();
	    User applit = userService.get(starter.getValue());
		InfoExport  info = new InfoExport();
		info.setApplicant(applit);
		info.setDepartment(applit.getDepartment());
		info.setStartDate(scal);
		info.setEndDate(DateTime.now());
		HTMLDocument form = entry.getHtmlDocument();
		info.setFormId(form==null?"-1":form.getId());
		OfficeDocument office = entry.getOfficeDocument();
		info.setOfficeId(office==null?"-1":office.getId());
		info.setTitle(form_title.getValue());
		Set<Attachment> atts = entry.getAttachment();
		for(Attachment t : atts){
			info.getAttachments().add(t.clone());
		}
		service.create(info);		
	}

	
	@Resource
	public void setService(InfoExportService service) {
		this.service = service;
	}

    @Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}  

}